#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :doubna_top250.py
# @Time      :2023/8/28 
# @Author    :CL
# @email     :1037654919@qq.com
import datetime

import requests

def douban_top250(url):

    import requests

    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Referer": "https://www.baidu.com/link?url=auxBciazX4_jCeK2YBRDMh5hJsfec03e74ZAri4iFOgoUObBMdaKWXuD5jkFU4IZ&wd=&eqid=eafa415100055cd80000000464ec53a8",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "cross-site",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\""
    }
    cookies = {
        "bid": "IoWM2l63OaQ",
        "ap_v": "0,6.0",
        "_pk_ref.100001.4cf6": "%5B%22%22%2C%22%22%2C1693212023%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DauxBciazX4_jCeK2YBRDMh5hJsfec03e74ZAri4iFOgoUObBMdaKWXuD5jkFU4IZ%26wd%3D%26eqid%3Deafa415100055cd80000000464ec53a8%22%5D",
        "_pk_id.100001.4cf6": "bfbb85ec3b2f9171.1693212023.",
        "_pk_ses.100001.4cf6": "1",
        "__utma": "223695111.25768331.1693212024.1693212024.1693212024.1",
        "__utmb": "223695111.0.10.1693212024",
        "__utmc": "223695111",
        "__utmz": "223695111.1693212024.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic"
    }
    # url = "https://movie.douban.com/top250"
    response = requests.get(url, headers=headers, cookies=cookies)

    # print(response.text)
    print(response)
    return response.text
proxies ={}
if __name__ == '__main__':
    lists = []
    for i in range(10):
        url = 'https://movie.douban.com/top250?start={}&filter='.format(i*25)
        response = douban_top250(url =url)
        # print(response)   # html
        from bs4 import BeautifulSoup
        soups = BeautifulSoup(response,'lxml')
        datas = soups.find('ol',class_ = 'grid_view').find_all('li')
        print( i,len(datas))
        # print(datas)

        for data in datas:
            try:
                name =  data.find('span',class_ = 'title').get_text().strip()
            except:
                name = ''
            try:
                playable = data.find('span',class_ = 'playable').get_text().strip()
            except:
                playable = ''
            try:
                actor = data.find('div',class_ = 'bd').find('p',class_ ='').get_text().strip()
            except:
                actor = ''
            try:
                star = data.find('div',class_ = 'star').find('span',class_ ='rating_num').get_text().strip()
            except:
                star = ''
            try:
                quote = data.find('p',class_ = 'quote').get_text().strip()
            except:
                quote = ''

            lists.append([name,playable,actor,star,quote])
            # lists.append(data.get_text())
    from pprint import pprint
    # pprint(lists)
    import pandas as pd

    df = pd.DataFrame(lists)  #,columns=['name','playable','actor','star','score','quote']
    df.to_excel(f'douban_top250_{datetime.datetime.today().strftime("%Y%m%d")}.xlsx')
# var = [['当幸福来敲门',
#        '[可播放]',
#        '导演: 加布里尔·穆奇诺 Gabriele Muccino\xa0\xa0\xa0主演: 威尔·史密斯 Will Smith ...\n'
#        '                            2006\xa0/\xa0美国\xa0/\xa0剧情 传记 家庭',
#        '9.2',
#        '平民励志片。'],
#  ['末代皇帝',
#   '[可播放]',
#   '导演: 贝纳尔多·贝托鲁奇 Bernardo Bertolucci\xa0\xa0\xa0主演: 尊龙 John Lone / 陈...\n'
#   '                            1987\xa0/\xa0英国 意大利 中国大陆 法国\xa0/\xa0剧情 传记 历史',
#   '9.3',
#   '“不要跟我比惨，我比你更惨”再适合这部电影不过了。']]